package com.dawn.trademanage.action;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.dawn.trademanage.bean.TradeDetailBean;
import com.dawn.trademanage.bean.TradeInfoBean;
import com.dawn.trademanage.service.TradeManageService;
import com.dawn.usermanage.bean.UserBean;
import com.dawn.usermanage.bean.UserPreference;
import com.dawn.usermanage.service.UserManageService;
import com.dawn.utils.CalculateTime;
import com.opensymphony.xwork2.ActionSupport;

@Controller("tradeAction")
@Scope("prototype")
public class TradeAction extends ActionSupport implements SessionAware,
		ServletRequestAware {

	private static final long serialVersionUID = 553819690946094426L;

	private Map<String, Object> session;
	private List<TradeInfoBean> tradeList;
	private HttpServletRequest request;
	private UserBean userBean;
	private InputStream inputStream;

	private int credit;
	private int reward;
	
	List<Map<String, Object>> result;

	@Autowired
	private TradeManageService tradeManageService;

	@Autowired
	private UserManageService userManageService;

	public String createTrade() {
		if (request.getParameter("type").equals("donothing")) {
			return "login";
		}
		String sinaId = session.get("sinaId").toString();
		UserBean userBean = userManageService.findUserBySinaId(sinaId);
		TradeInfoBean tradeInfo = userBean.getTtTradeinfo();
		tradeInfo.setCreditRate(credit);
		tradeInfo.setUnitPrice(reward);
		if (request.getParameter("type").equals("first")) {
			tradeInfo.setLastBeginTime(Calendar.getInstance());
			tradeInfo.setIsStop((byte) 0);
			tradeManageService.updateTrade(tradeInfo);
			return "login";
		}
		tradeManageService.updateTrade(tradeInfo);
		return SUCCESS;
	}

	public String showTradeList() {
		String sinaId = session.get("sinaId").toString();
		userBean = userManageService.findUserBySinaId(sinaId);
		tradeList = tradeManageService.showTradeList();
		if (null == tradeList)
			return ERROR;
		tradeList.remove(userBean.getTtTradeinfo());
		return SUCCESS;
	}

	public String main() {
		String sinaId = session.get("sinaId").toString();
		userBean = userManageService.findUserBySinaId(sinaId);
		return SUCCESS;
	}

	public String setTradeInfo() {
		String sinaId = session.get("sinaId").toString();
		userBean = userManageService.findUserBySinaId(sinaId);
		return SUCCESS;
	}

	public String getBalance() throws UnsupportedEncodingException {
		String sinaId = session.get("sinaId").toString();
		UserBean user = userManageService.findUserBySinaId(sinaId);
		if (user == null)
			return ERROR;
		String balance = String.valueOf(user.getTtTradeinfo().getBalance());
		this.inputStream = new ByteArrayInputStream(balance.getBytes("UTF-8"));
		return SUCCESS;
	}

	public String setTradeStatus() throws UnsupportedEncodingException {
		long tradeId = Long.parseLong(request.getParameter("tradeId"));
		String result = null;
		try {
			if (request.getParameter("type").equals("true")) {
				tradeManageService.beginTrade(tradeId);
				result = "ok";
			} else if (request.getParameter("type").equals("false")) {
				tradeManageService.stopTrade(tradeId);
				result = "ok";
			} else
				result = "设置失败";
		} catch (Exception e) {
			result = "设置失败";
		}
		this.inputStream = new ByteArrayInputStream(result.getBytes("UTF-8"));
		return SUCCESS;
	}

	public String listFollowers() {
		UserPreference user = (UserPreference)session.get("user");
		List<TradeDetailBean> list = tradeManageService.listFollowers(user.getUserId());
		result = new ArrayList<Map<String, Object>>();
		for(TradeDetailBean bean : list){
			UserBean userBean = userManageService.findUserByUID(bean.getFansId());
			Map<String,Object> map = new HashMap<String,Object>();
			String datetime = CalculateTime.getDateString(bean.getFollowedTime());
			map.put("date", datetime);
			map.put("userInfo", userBean);
			result.add(map);
		}
		return SUCCESS;
	}

	public String listIdols() {
		UserPreference user = (UserPreference)session.get("user");
		List<TradeDetailBean> list = tradeManageService.listIdols(user.getUserId());
		result = new ArrayList<Map<String, Object>>();
		for(TradeDetailBean bean : list){
			UserBean userBean = userManageService.findUserByUID(bean.getTtTradeinfo().getTuUserinfo().getUserId());
			Map<String,Object> map = new HashMap<String,Object>();
			String datetime = CalculateTime.getDateString(bean.getFollowedTime());
			map.put("date", datetime);
			map.put("userInfo", userBean);
			result.add(map);
		}
		return SUCCESS;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public List<TradeInfoBean> getTradeList() {
		return tradeList;
	}

	public void setTradeList(List<TradeInfoBean> tradeList) {
		this.tradeList = tradeList;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public int getCredit() {
		return credit;
	}

	public void setCredit(int credit) {
		this.credit = credit;
	}

	public int getReward() {
		return reward;
	}

	public void setReward(int reward) {
		this.reward = reward;
	}

	public UserBean getUserBean() {
		return userBean;
	}

	public void setUserBean(UserBean userBean) {
		this.userBean = userBean;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public List<Map<String, Object>> getResult() {
		return result;
	}

	public void setResult(List<Map<String, Object>> result) {
		this.result = result;
	}
}
